/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameObject;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import javax.swing.Timer;
import javax.imageio.ImageIO;

/**
 *
 * @author NGUYEN VAN TAM
 */
public class Card extends VisibleGameEntity{
    public CardAttribute _attributes;
    // chưa khởi tạo attribute cho lá bài
    public Card()
    {
        _attributes = new CardAttribute();
    }
    
    @Override
    public boolean Init(int _numberSprite[], int _Width,int _Heigth,String StrResource[],int isprite, int nDelay)
    {
        _nsprite = _numberSprite.length;
        _sprite = new Sprite[_nsprite];
        _width = _Width;
        _height = _Heigth;
        this._nDelay = nDelay;

        for (int i = 0; i < _nsprite; i++)
        {
            try
            {
                Image[] image = new Image[_numberSprite[i]];

                for (int j = 0; j < _numberSprite[i]; j++)
                {
                    image[j] = ImageIO.read(new File(StrResource[i]));
                }
                _sprite[i] = new Sprite(0, 0, _Width,_Heigth ,image);
                _isprite = isprite;
            }
            catch(IOException ex)
            {
                return false;
            }
        }
        return true;
    }
    
    @Override
    public VisibleGameObject Clone()
    {
        VisibleGameObject obj = new Card();
        obj._nsprite = this._nsprite;
        obj._x = this._x;
        obj._y = this._y;
        obj._isprite = this._isprite;
        obj._width = this._width;
        obj._height = this._height;
        obj._nDelay = 0;       
        ((Card)obj)._attributes = this._attributes.Clone();
        
        for(int i = 0;i < this._sprite.length;i++)
        {
            obj._sprite[i] = new Sprite(this._x, this._y, this._sprite[i]._width, this._sprite[i]._height, this._sprite[i]._images);
        }
        return obj;
    }
    
    @Override
    public void Update(Timer gameTime)
    {
        if(this._attributes._open == true)
        {
            this._isprite = 1;
        }

        // xữ lí sự kiện trên nếu chuột click trên lá bài  
        if(this._attributes._open == true)
        {
            if(HandlingInput.MouseHanller.MouseLeftClick(new Rectangle(this._x,this._y,this._attributes._rectShow.width ,this._attributes._rectShow.height)))
            {
                if(this._attributes._state == SettingGameObject.CARDSELECTED)
                    this._attributes._state = SettingGameObject.CARDNONE;
                else
                    this._attributes._state = SettingGameObject.CARDSELECTED;
                HandlingInput.MouseHanller._currentMouse = null;
            }
            else if(HandlingInput.MouseHanller.MouseOver(new Rectangle(this._x,this._y,this._attributes._rectShow.width ,this._attributes._rectShow.height)))
            {
                if(this._attributes._state != SettingGameObject.CARDSELECTED)
                    this._attributes._state = SettingGameObject.CARDHOVER;
                HandlingInput.MouseHanller._currentMouse = null;
            } 
            else 
            {
                if(this._attributes._state != SettingGameObject.CARDSELECTED && this._attributes._state == SettingGameObject.CARDHOVER)
                    this._attributes._state = SettingGameObject.CARDNONE;
                //HandlingInput.MouseHanller._currentMouse = null;
            }
        }
        Update(gameTime, _x, _y, this._width,this._height);
    }
    
    @Override
    public void Update(Timer gameTime,int x, int y, int w, int h)
    {

            this._width = w;
            this._height = h;
            this._x = x;
            this._y = y;
            
            for (int i = 0; i < _sprite.length; i++)
            {
                _sprite[i].update(gameTime,x,y,w,h,_sprite[i]._selectedIndex);
            }
    }
    
    //
    @Override
    public void Draw(Timer gameTime,Graphics2D graphic)
    {
        /*
        for (int i = 0; i <  _sprite.length; i++)
        {
            _sprite[i].Draw(gameTime,graphic);
        }*/       

        if(_isprite >= 0 && _isprite < _sprite.length)
            _sprite[_isprite].Draw(gameTime,graphic);
    }
    
    public void SetRectangleShow(Rectangle rec)
    {
        this._attributes._rectShow.x = rec.x;
        this._attributes._rectShow.y = rec.y;
        this._attributes._rectShow.width = rec.width;
        this._attributes._rectShow.height = rec.height;
    }
}
